package com.ls.fw.office.itext.itext.builder;

import java.awt.Color;
import java.io.IOException;
import java.util.Map;

import com.lowagie.text.Chunk;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAction;
import com.ls.fw.commons.core.utils.string.StringUtils;

/**
 * 
 * @author defender
 *
 */
public class ParagraphBuilder{
	static BaseFont zh_bf ;
	static{
		 try {
			 zh_bf = BaseFont.createFont("STSongStd-Light",  
			        "UniGB-UCS2-H", false);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
	}
    static final Font smallBoldTableFont = new Font(Font.HELVETICA, 6, Font.BOLD, Color.BLACK);
    static final Font smallNormalTableFont = new Font(Font.HELVETICA, 6, Font.NORMAL, Color.BLACK);
    static final Font h1HeadingFont=new Font(Font.HELVETICA, 20, Font.BOLD, Color.BLUE);
    static final Font h2HeadingFont=new Font(Font.HELVETICA, 15, Font.BOLD, Color.BLUE);
    static final Font smallFont=new Font(Font.HELVETICA, 9, Font.NORMAL, Color.BLACK);
    static final Font boldFont=new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
    static final Font normalFont=new Font(Font.HELVETICA, 12, Font.NORMAL, Color.BLACK);
    static final Font normalLinkFont=new Font(Font.HELVETICA, 12, Font.UNDERLINE, Color.BLUE);

    static final Font zh_bold_font = new Font(zh_bf, 12, Font.BOLD,  
    		Color.BLACK);  
    static final Font zh_italic_font = new Font(zh_bf, 12, Font.ITALIC,  
    		Color.BLACK);  
    static final Font zh_impressFont = new Font(zh_bf, 16, Font.BOLDITALIC,  
    		Color.BLACK);  
    
    static final Font zh_smallBoldTableFont = new Font(zh_bf, 6, Font.BOLD, Color.BLACK);
    static final Font zh_mallNormalTableFont = new Font(zh_bf, 6, Font.NORMAL, Color.BLACK);
    static final Font zh_h1HeadingFont = new Font(zh_bf, 20, Font.BOLD, Color.BLUE);
    static final Font zh_h2HeadingFont = new Font(zh_bf, 15, Font.BOLD, Color.BLUE);
    static final Font zh_smallFont = new Font(zh_bf, 9, Font.NORMAL, Color.BLACK);
    static final Font zh_boldFont = new Font(zh_bf, 12, Font.BOLD, Color.BLACK);
    static final Font zh_normalFont = new Font(zh_bf, 12, Font.NORMAL, Color.BLACK);
    static final Font zh_normalLinkFont = new Font(zh_bf, 12, Font.UNDERLINE, Color.BLUE);

    private String text;
    private int alignment;
    private Font font;
    private float spacingAfterUnits;
    private String marker;
    private String goToMarker;
    private float spacingBeforeUnits;
    private String linkFilename;
    private int linkFilePageNumber;


    public String getText() {
		return text;
	}

	public ParagraphBuilder setText(String text) {
		this.text = text;
		 return this;
	}

	public int getAlignment() {
		return alignment;
	}

	public ParagraphBuilder setAlignment(int alignment) {
		this.alignment = alignment;
		 return this;
	}

	public Font getFont() {
		return font;
	}

	public ParagraphBuilder setFont(Font font) {
		this.font = font;
		 return this;
	}

	public float getSpacingAfterUnits() {
		return spacingAfterUnits;
	}

	public ParagraphBuilder setSpacingAfterUnits(float spacingAfterUnits) {
		this.spacingAfterUnits = spacingAfterUnits;
		 return this;
	}

	public String getMarker() {
		return marker;
	}

	public ParagraphBuilder setMarker(String marker) {
		this.marker = marker;
		 return this;
	}

	public String getGoToMarker() {
		return goToMarker;
	}

	public ParagraphBuilder setGoToMarker(String goToMarker) {
		this.goToMarker = goToMarker;
		 return this;
	}

	public float getSpacingBeforeUnits() {
		return spacingBeforeUnits;
	}

	public ParagraphBuilder setSpacingBeforeUnits(float spacingBeforeUnits) {
		this.spacingBeforeUnits = spacingBeforeUnits;
		 return this;
	}

	public String getLinkFilename() {
		return linkFilename;
	}

	public ParagraphBuilder setLinkFilename(String linkFilename) {
		this.linkFilename = linkFilename;
		 return this;
	}

	public int getLinkFilePageNumber() {
		return linkFilePageNumber;
	}

	public ParagraphBuilder setLinkFilePageNumber(int linkFilePageNumber) {
		this.linkFilePageNumber = linkFilePageNumber;
		 return this;
	}

	public ParagraphBuilder(String text) {
        this.text = text;
    }

    public ParagraphBuilder withMarker(String marker){
        this.marker = marker;
        return this;
    }

    public ParagraphBuilder withGoToMarker(String marker){
        this.goToMarker = marker;
        return this;
    }

    public ParagraphBuilder asH1Heading(){
        font=h1HeadingFont;
        return this;
    }

    public ParagraphBuilder asZH_H2Heading(){
        font=zh_h2HeadingFont;
        return this;
    }

    public ParagraphBuilder asZH_H1Heading(){
        font=zh_h1HeadingFont;
        return this;
    }

    public ParagraphBuilder asH2Heading(){
        font=h2HeadingFont;
        return this;
    }

    public ParagraphBuilder withLeftAlignment(){
        alignment= Element.ALIGN_LEFT;
        return this;
    }

    public ParagraphBuilder withRightAlignment(){
        alignment= Element.ALIGN_RIGHT;
        return this;
    }

    public ParagraphBuilder withCenterAlignment(){
        alignment= Element.ALIGN_CENTER;
        return this;
    }

    public ParagraphBuilder withJustifyAlignment(){
        alignment=Element.ALIGN_JUSTIFIED;
        return this;
    }

    public ParagraphBuilder withSmallTableFont(){
        font=smallNormalTableFont;
        return this;
    }

    public ParagraphBuilder withSmallTableBoldFont(){
        font=smallBoldTableFont;
        return this;
    }

    public ParagraphBuilder withSmallFont(){
        font=smallFont;
        return this;
    }

    public ParagraphBuilder withBoldFont(){
        font=boldFont;
        return this;
    }

    public Paragraph getParagraph(Map<String, Integer> toc, Integer pageNum) {
        Chunk chunk = new Chunk(StringUtils.defaultString(text), font == null ? zh_normalFont : font);
        if(marker!=null && toc!=null){
            chunk.setLocalDestination(marker);
            toc.put(marker, pageNum);
        }else if(goToMarker!=null){
            chunk.setLocalGoto(goToMarker);
        }else if(linkFilename!=null){
            chunk.setAction(new PdfAction(linkFilename, linkFilePageNumber));
        }
        Paragraph paragraph=new Paragraph(chunk);
        paragraph.setAlignment(alignment);
        if(spacingAfterUnits>0)  paragraph.setSpacingAfter(spacingAfterUnits);
        if(spacingBeforeUnits>0) paragraph.setSpacingBefore(spacingBeforeUnits);
        return paragraph;
    }

    Paragraph getParagraph() {
        return getParagraph(null, null);
    }

    public ParagraphBuilder withSpacingAfter(float units){
        spacingAfterUnits = units;
        return this;
    }


    public ParagraphBuilder withSpacingBefore(float units){
        spacingBeforeUnits = units;
        return this;
    }

    public ParagraphBuilder asLinkToAnotherFile(String linkFilename, int linkFilePageNumber){
        this.linkFilename = linkFilename;
        this.linkFilePageNumber = linkFilePageNumber==0?1:linkFilePageNumber;
        this.font=normalLinkFont;
        return this;
    }

    public static ParagraphBuilder newParagraph(String text){
        return new ParagraphBuilder(text);
    }

    public static ParagraphBuilder newParagraph(Object value){
        return new ParagraphBuilder(String.valueOf(value));
    }
}
